﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using SmartMenu.Classes;
using SmartMenu.Controllers;
using visinia.SmartEngine;
using visinia.SmartEngine.Installer;
using visinia.SmartEngine.SiteMap;

public partial class Admin_Pages__Menu_MenuItem_MenuItem : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPluginUrls();
        if (Request.QueryString["MenuItemId"] != null)
        { 
            int menuItemId = 0;
            if (Int32.TryParse(Request.QueryString["MenuItemId"], out menuItemId))
            {
                MenuItemInfo menuItem = MenuItemController.GetMenuItem(menuItemId);
                if (menuItem != null)
                {
                    TITLE.Text = menuItem.Title;
                    if (!string.IsNullOrEmpty(menuItem.Image))
                    {
                        if (menuItem.Image.StartsWith("externallink:", StringComparison.OrdinalIgnoreCase))
                            IMAGE.Text = menuItem.Image.Remove(0, 13);
                        else
                            IMAGE.Text = WebRoots.GetWebRoot + menuItem.Image;
                    }
                    ORDER.Text = menuItem.Order.ToString();
                    MENU_VISIBLE.Checked = menuItem.Visible;
                    if(menuItem.Url.StartsWith("externallink:", StringComparison.OrdinalIgnoreCase))
                        URL.Text = menuItem.Url.Remove(0, 13);
                    else
                        URL.Text = WebRoots.GetWebRoot + menuItem.Url;
                    Menus.SelectedValue = menuItem.MenuId.ToString();
                }
            }
        }
    }

    private void BindPluginUrls()
    {        
        foreach (Plugin plugin in PluginController.GetInstalledPlugins())
        {
            List<SiteMapInfo> list = SiteMapController.GetPluginSiteMapData().FindAll(p => string.Compare(p.Plugin, plugin.Name, true) == 0);
            if (list == null)
                continue;
            if (list.Count == 0)
                continue;
            PluginList.Items.Add(plugin.Name);
            LiteralControl divStart = new LiteralControl("<div class=\"" + plugin.Name + "\">");
            UrlList.Controls.Add(divStart);  
            foreach (SiteMapInfo sitemapInfo in list)
            {
                LiteralControl anchor = new LiteralControl("<a>" + sitemapInfo.Url + "</a><br/>");
                UrlList.Controls.Add(anchor);                
            }
            LiteralControl divEnd = new LiteralControl("</div>");
            UrlList.Controls.Add(divEnd);  
        }
    }

    protected void SAVE_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["MenuItemId"] != null)
        {
            int menuItemId = 0;
            if (Int32.TryParse(Request.QueryString["MenuItemId"], out menuItemId))
            {
                MenuItemInfo menuItem = MenuItemController.GetMenuItem(menuItemId);
                if (menuItem != null)
                {
                    if (!string.IsNullOrEmpty(URL.Text))
                    {
                        string root = WebRoots.GetWebRoot;
                        menuItem.Title = TITLE.Text;
                        if (!string.IsNullOrEmpty(IMAGE.Text))
                        {
                            if (IMAGE.Text.StartsWith(WebRoots.GetWebRoot))
                                menuItem.Image = IMAGE.Text.Remove(0, root.Length);
                            else
                                menuItem.Image = "externallink:" + IMAGE.Text;
                        }
                        int order = 0;
                        Int32.TryParse(ORDER.Text, out order);
                        menuItem.Order = order;
                        menuItem.Visible = MENU_VISIBLE.Checked;
                        if (URL.Text.StartsWith(WebRoots.GetWebRoot))
                            menuItem.Url = URL.Text.Remove(0, root.Length);
                        else
                            menuItem.Url = "externallink:" + URL.Text;
                        menuItem.MenuId = Convert.ToInt32(Menus.SelectedValue);
                        MenuItemController.UpdateMenuItem(menuItem);
                    }
                }
            }
        }
        else
        {
            if (!string.IsNullOrEmpty(URL.Text))
            {
                MenuItemInfo menuItem = new MenuItemInfo();
                string root = WebRoots.GetWebRoot;
                menuItem.Title = TITLE.Text;
                if (!string.IsNullOrEmpty(IMAGE.Text))
                {
                    if (IMAGE.Text.StartsWith(WebRoots.GetWebRoot))
                        menuItem.Image = IMAGE.Text.Remove(0, root.Length);
                    else
                        menuItem.Image = "externallink:" + IMAGE.Text;
                }
                int order = 0;
                Int32.TryParse(ORDER.Text, out order);
                menuItem.Order = order;
                menuItem.Visible = MENU_VISIBLE.Checked;
                if (URL.Text.StartsWith(WebRoots.GetWebRoot))
                    menuItem.Url = URL.Text.Remove(0, root.Length);
                else
                    menuItem.Url = "externallink:" + URL.Text;
                menuItem.MenuId = Convert.ToInt32(Menus.SelectedValue);
                MenuItemController.UpdateMenuItem(menuItem);
                MenuItemController.AddMenuItem(menuItem);
            }
        }
    }
    protected void CANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
}